#include "mid_camera.h"
// lib
#include "debug.h"
// bsp
#include "bsp_v4l2.h"
// #include "bsp_gpio.h"
// ...
#include <linux/videodev2.h>

#define USE_FRAME_NUM                       10

const char *mid_get_cam_err_str(int32_t err_code) {
    return bsp_cam_get_err_str(err_code);
}

/// @brief 获取照片
/// @param tData 配置参数
/// @return <0-fail, >=0-success
int32_t mid_cam_capture_photo(camera_set_t tData) {
    camera_error_t tRet  = err_none;
    camera_error_t tRet2 = err_none;
    v4l2_parm_t g_V4l2Parm = {
        .sDevName = "/dev/video0",
        .sPhotoDir = OUTPUT_FILE,

        .iCameraId = -1,

        .eIoMethod = IO_METHOD_MMAP,
        .bUseDefault = false,
        .uWidth = 8000,
        .uHeight = 6000,
        .uPixelFormat = V4L2_PIX_FMT_MJPEG,                     // 设置格式为MJPEG以支持更高分辨率
        .iUseNum = USE_FRAME_NUM,

        .uBufNum = 0,
        .pBufs = NULL,
    };

    // FILL_LIGHT_ON();                                            // 开补光灯



    // 执行视频采集的主要步骤
    tRet = bsp_cam_open(&g_V4l2Parm);                           // 1. 打开设备
    if (tRet != err_none) goto safe_exit;
    tRet = bsp_cam_init(&g_V4l2Parm);                           // 2. 初始化设备
    if (tRet != err_none) goto safe_exit;
    tRet = bsp_cam_get_all_parm_and_val(&g_V4l2Parm);           // 2.1 获取所有详细控制参数
    if (tRet != err_none) goto safe_exit;
    tRet = bsp_cam_get_all_cur_val(&g_V4l2Parm);                // 2.x 查询部分控制参数的当前值
    if (tRet != err_none) goto safe_exit;

    #if 1
    while (tData.iContrast-- > 0) {
        char sPhotoDir[128] = {0};
        sprintf(sPhotoDir, "/root/ruitong/photo_%d.jpg", tData.iContrast);
        g_V4l2Parm.sPhotoDir = sPhotoDir;
    #endif
    // tRet = bsp_cam_set_all_val(&g_V4l2Parm);                    // 2.x 设置部分支持的控制参数
    // if (tRet != err_none) goto safe_exit;
    tRet = bsp_cam_set_parm(&g_V4l2Parm, cam_func_auto_focus, tData.tAutoFocus);
    if (tRet != err_none) goto safe_exit;
    tRet = bsp_cam_set_parm(&g_V4l2Parm, cam_func_focus_absolute, tData.iFocus);
    if (tRet != err_none) goto safe_exit;
    tRet = bsp_cam_set_parm(&g_V4l2Parm, cam_func_contrast, tData.iContrast);
    if (tRet != err_none) goto safe_exit;
    tRet = bsp_cam_get_all_cur_val(&g_V4l2Parm);                // 2.x 查询部分控制参数的当前值
    if (tRet != err_none) goto safe_exit;
    tRet = bsp_start_capturing(&g_V4l2Parm);                    // 3. 开始采集
    if (tRet != err_none) goto safe_exit;
    tRet = bsp_mainloop(&g_V4l2Parm);                           // 4. 主循环捕获指定帧数
    if (tRet != err_none) goto safe_exit;
    tRet2 = bsp_stop_capturing(&g_V4l2Parm);                    // 5. 停止采集
    if (tRet != err_none) goto safe_exit;
    #if 1
    }
    #endif

    safe_exit:
    tRet2 = bsp_uninit_camera(&g_V4l2Parm);                     // 6. 释放设备资源
    tRet2 = bsp_close_camera(&g_V4l2Parm);                      // 7. 关闭设备
    if (tRet != err_none || tRet2 != err_none) goto err_return;



    // FILL_LIGHT_OFF();                                           // 关补光灯
    return err_none;
    err_return:
    // FILL_LIGHT_OFF();                                           // 关补光灯
    _DEBUG_PRINTF(DBG_ERR, "ErrRet:%s. %s\r\n", 
        mid_get_cam_err_str(tRet),
        tRet2==err_none ? "" : mid_get_cam_err_str(tRet2));
    return tRet;
}